package com.jdew.thkj.fragment;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.activity.LoginActivity;
import com.jdew.thkj.activity.OrderMingXiActivity;
import com.jdew.thkj.adapter.JDOrderAdapter;
import com.jdew.thkj.base.BaseFragment;
import com.jdew.thkj.bean.JDOrderBean;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.onitemInterface.OnItemClickListener;
import com.jdew.thkj.utils.DeviceUtils;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.ToastUtils;
import com.jdew.thkj.utils.WeiboDialogUtils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangzai on 2018/5/27.
 */

public class JDOrderFailedFragment extends BaseFragment {
    @Bind(R.id.recyclerview)
    RecyclerView recycleView;
    @Bind(R.id.smartRefresh)
    SmartRefreshLayout smartRefreshLayout;
    @Bind(R.id.noresult)
    TextView noresult;
    @Bind(R.id.result_view)
    RelativeLayout resultView;
    private JDOrderAdapter adapter;
    private OrderMingXiActivity activity;
    private List<JDOrderBean.DataBean> list = new ArrayList<>();
    private int page = 1;
    private String ItemId;

    @Override
    protected int getContentViewId() {
        return R.layout.order_all;
    }

    @Override
    protected void onCreateViewT() {
        activity = (OrderMingXiActivity) getActivity();
        adapter = new JDOrderAdapter(activity, list);
        sp = activity.getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        setRcycleview(recycleView, adapter);
        if(list.size()==0){
            smartRefreshLayout.setVisibility(View.GONE);
            resultView.setVisibility(View.VISIBLE);
            noresult.setText("您还没有订单哦");
        }
        smartRefreshLayout.setRefreshHeader(new MaterialHeader(activity));
        smartRefreshLayout.setRefreshFooter(new BallPulseFooter(activity).setSpinnerStyle(SpinnerStyle.Scale));
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                GetOrderDetail();
                smartRefreshLayout.finishLoadMore();
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                smartRefreshLayout.setEnableLoadMore(true);
                GetOrderDetail();
                smartRefreshLayout.finishRefresh();
            }
        });
        adapter.setItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (!RegexValidateUtils.isFastClick()) {
                    return;
                }
                if (sp.getString("SpareTwo", "").equals("")) {
                    startActivity(new Intent(activity, LoginActivity.class));
                } else if (sp.getString("UserSjPhone", "").equals("") && (!sp.getString("SpareTwo", "").equals("1"))) {
                    activity.toset("检测到您还没有绑定上级,请到我的页面绑定上级");
                } else {
                    ItemId = list.get(position).getSkuId();
                    activity.toJDDetail(ItemId);
                }
            }
        });
    }

    @Override
    public void initData() {
        GetOrderDetail();
    }

    public void GetOrderDetail() {
        if (!DeviceUtils.isNetworkAvalible(activity)) {
            if (!activity.isFinishing()) {
                ToastUtils.getInstance(activity).showMessage("网络连接错误请检查!");
            }
            return;
        }
        if (null == activity.mWeiboDialog || !activity.mWeiboDialog.isShowing()) {
            activity.mWeiboDialog = WeiboDialogUtils.createLoadingDialog(activity);
        }
        HashMap<String, Object> map = new HashMap();
        map.put("UserId", sp.getString("userId", ""));
        map.put("PageIndex", page);
        map.put("validCode", -1);
        String timesamp = RegexValidateUtils.getTime13();
        String numsamp = RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).JdGetOrder(map, RegexValidateUtils.getUniquePsuedoID(), timesamp, numsamp, MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp + numsamp + RegexValidateUtils.getUniquePsuedoID() + sp.getString("SignToken", "") + "PageIndex" + page + "UserId" + sp.getString("userId", "") + "validCode-1")).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JDOrderBean>() {
            @Override
            public void onCompleted() {
                WeiboDialogUtils.closeDialog(activity.mWeiboDialog);
            }

            @Override
            public void onError(Throwable e) {
                WeiboDialogUtils.closeDialog(activity.mWeiboDialog);
            }

            @Override
            public void onNext(JDOrderBean bean) {
                WeiboDialogUtils.closeDialog(activity.mWeiboDialog);
                if (bean.getCode().equals("0")) {
                    if (null == bean.getData() && page == 1) {
                        noresult.setText("您还没有订单哦!");
                        return;
                    } else {
                        if (bean.getData().size() == 0) {
                            smartRefreshLayout.setEnableLoadMore(false);
                        }
                    }
                    smartRefreshLayout.setVisibility(View.VISIBLE);
                    if (page == 1) {
                        list.clear();
                    }
                    list.addAll(bean.getData());
                    adapter.setDataList(list);
                    if (bean.getData().size() < 30 && bean.getData().size() > 0) {
                        smartRefreshLayout.setEnableLoadMore(false);
                    } else {
                        if (list.size() == 0 && page == 1) {
                            smartRefreshLayout.setVisibility(View.GONE);
                            resultView.setVisibility(View.VISIBLE);
                            noresult.setText("您还没有订单哦");
                            return;
                        }
                    }
                } else if (bean.getCode().equals("-1") && page == 1) {
                    smartRefreshLayout.setVisibility(View.GONE);
                    resultView.setVisibility(View.VISIBLE);
                    noresult.setText("您还没有订单哦");
                } else {
                    smartRefreshLayout.setEnableLoadMore(false);
                }
            }
        });
    }
}
